package com.smtech.xz.oa;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.github.lzyzsd.jsbridge.BridgeHandler;
import com.github.lzyzsd.jsbridge.BridgeWebView;
import com.github.lzyzsd.jsbridge.BridgeWebViewClient;
import com.github.lzyzsd.jsbridge.CallBackFunction;
import com.smtech.xz.util.SPUtils;
import com.wang.avi.AVLoadingIndicatorView;

import static com.smtech.xz.api.URLConstant.URL;
import static com.smtech.xz.util.SPUtils.NET_URL;

/**
 * @author Administrator
 */
public class WebActivity extends MainActivity {

    private BridgeWebView webView;
    private AVLoadingIndicatorView loading;
    private String curURL;

//    Handler tmpHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
        webView = findViewById(R.id.adv_webview);
        loading = findViewById(R.id.avi_web);

        curURL = SPUtils.get(NET_URL, "");
        if (TextUtils.isEmpty(curURL)) {
            curURL = URL;
        }
//        webView.setVisibility(View.INVISIBLE);
//        loading.setVisibility(View.VISIBLE);

        initWebView();
//        tmpHandler.postDelayed(runnable, 1000);

    }

    private void initWebView() {
        //请尽量在设置webView属性之前调用，如果在webView属性之后调用体验可能不如这样好
        LocationHelper.getLocationClient().startAssistantLocation(webView);

        WebSettings settings = webView.getSettings();
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setJavaScriptEnabled(true);
        settings.setTextZoom(100);
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        webView.addJavascriptInterface(new JsInterface(this), "android");
        webView.registerHandler("smshare", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                share(data);
            }
        });
        settings.setDomStorageEnabled(true);

        settings.setAllowContentAccess(true);
        settings.setAllowFileAccess(true);
        settings.setAllowFileAccessFromFileURLs(true);
        settings.setAllowUniversalAccessFromFileURLs(true);

        settings.setDatabaseEnabled(true);
        //设置定位的数据库路径
        String dir = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
        settings.setGeolocationDatabasePath(dir);

        settings.setGeolocationEnabled(true);
        String userAgent = settings.getUserAgentString();
        userAgent =  userAgent+ " KnxAndroidApp/1.0";
        settings.setUserAgentString(userAgent);
        Log.i("tag", "userAgent="+userAgent);
        webView.setWebViewClient(new WebViewClient(webView));
        webView.setWebChromeClient(new MyWebChromeClient());
        webView.loadUrl(curURL);
    }


    public class WebViewClient extends BridgeWebViewClient {

        public WebViewClient(BridgeWebView webView) {
            super(webView);
        }


        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            webView.setVisibility(View.INVISIBLE);
            loading.setVisibility(View.VISIBLE);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            boolean loading = super.shouldOverrideUrlLoading(view, url);
            if (!loading) {
                webView.loadUrl(url);
                loading = true;
            }
            return loading;
        }

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            super.onReceivedError(view, request, error);
            if (request.isForMainFrame()) {
                showErrorView();
            }
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            webView.setVisibility(View.VISIBLE);
            loading.setVisibility(View.GONE);
        }
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return super.onKeyDown(keyCode, event);
    }
}
